home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 351-375 / disk_351 / pdc / libsrc.lzh / LibSrc / SysIO / open.c < prev    next >
C/C++ Source or Header  |  1990-04-07  |  3KB  |  123 lines

  1. /*
  2.  * Libraries and headers for PDC release 3.3 (C) 1989 Lionel Hummel.
  3.  * PDC Software Distribution (C) 1989 Lionel Hummel and Paul Petersen.
  4.  * PDC I/O Library (C) 1987 by J.A. Lydiatt.
  5.  *
  6.  * This code is freely redistributable upon the conditions that this 
  7.  * notice remains intact and that modified versions of this file not
  8.  * be included as part of the PDC Software Distribution without the
  9.  * express consent of the copyright holders.  No warrantee of any
  10.  * kind is provided with this code.  For further information, contact:
  11.  *
  12.  *  PDC Software Distribution    Internet:                     BIX:
  13.  *  P.O. Box 4006             or hummel@cs.uiuc.edu            lhummel
  14.  *  Urbana, IL  61801-8801       petersen@uicsrd.csrd.uiuc.edu
  15.  */
  16.  
  17. /* open.c - 
  18.  *    creat(char *name, int pmode)    - creates a new file
  19.  *    open(char *name, int mode)    - opens a file
  20.  */
  21.  
  22. #include    <errno.h>
  23. #include    <fcntl.h>
  24. #include    <exec/types.h>
  25. #include        <functions.h>
  26. #include    <libraries/dos.h>
  27.  
  28. /*
  29.  * creates a new file with the requested file modes
  30.  */
  31. creat(name, pmode)
  32. char *name;
  33. int   pmode;
  34. {
  35.     return( open(name, O_WRONLY|O_TRUNC|O_CREAT, pmode));
  36. }
  37.  
  38. /*
  39.  * returns the max. number of entries available in the file descrptor table
  40.  */
  41.  
  42. int getdtablesize()
  43. {
  44.     extern int _numdev;
  45.  
  46.     return(_numdev);
  47. }
  48.  
  49. /*
  50.  open a file for unbuffered I/O.
  51.  
  52.   open(path, flags, mode)
  53.   char *path;
  54.   int   flags;
  55.   int   mode;
  56.  
  57.  The file mode flag is presently ignored - This may not be the case in
  58.  the future, so it is recommended that you use the value DEF_PMODE defined
  59.  in fcntl.h if you have the O_CREAT flag set.
  60.  */
  61.  
  62. open(path, flags, mode)
  63. char *path;
  64. int   flags;
  65. int   mode;
  66. {
  67.     struct _device *p;
  68.     int fd, err;
  69.     struct FileHandle *fh;
  70.     long lib;
  71.     extern void Chk_Abort();
  72.     extern struct _device *_devtab;
  73.  
  74.     Chk_Abort();
  75.  
  76.     /*
  77.      * Find the first empty entry in the device table.
  78.      */
  79.  
  80.     p = &_devtab[0];
  81.  
  82.     for ( fd=0; fd < _numdev; ++fd )
  83.         if ( !p[fd]._fileHandle ) break;
  84.  
  85.     if ( fd >= _numdev ){
  86.         errno = EMFILE;
  87.         return(-1);
  88.     }
  89.  
  90.     if (flags & O_TRUNC) {
  91.         if (fh = Lock(path, ACCESS_WRITE)) {
  92.             UnLock(fh);
  93.             if ( !DeleteFile(path) ){
  94.                  errno = IoErr();
  95.                 return(-1);
  96.             }
  97.         }
  98.     }
  99.  
  100.     fh = Open(path, MODE_OLDFILE);
  101.     if ( !fh ) {
  102.         if ( !(flags&O_CREAT)) {
  103.             errno = ENOENT;
  104.             return(-1);
  105.         }
  106.         if ((fh = Open(path, MODE_NEWFILE)) == 0) {
  107.             errno = IoErr();
  108.             return(-1);
  109.         }
  110.     }
  111.     else if ((flags&(O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL)) {
  112.         Close(fh);
  113.         errno = EEXIST;
  114.         return(-1);
  115.     }
  116.     p[fd]._fileHandle = fh;
  117.     p[fd]._mode = flags;
  118.     if (flags & O_APPEND)
  119.         Seek(fh, 0L, OFFSET_END);
  120.     return(fd);
  121.  
  122. }
  123.